package com.scrx.ymg.dao.head;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.scrx.ymg.dao.agent.IAgentInfoDao;
import com.scrx.ymg.dao.base.BaseDaoImpl;
import com.scrx.ymg.dao.direct.IDirectDao;
import com.scrx.ymg.dao.seckill.ISeckillDao;
import com.scrx.ymg.dao.supermarket.ISuperMarketDao;
import com.scrx.ymg.entity.AgentDrawings;
import com.scrx.ymg.entity.AgentInfo;
import com.scrx.ymg.entity.BankAccount;
import com.scrx.ymg.entity.DirectInfo;
import com.scrx.ymg.entity.DividedInto;
import com.scrx.ymg.entity.GoodsType;
import com.scrx.ymg.entity.HeadMoney;
import com.scrx.ymg.entity.HeadWithdarw;
import com.scrx.ymg.entity.OtherInfo;
import com.scrx.ymg.entity.SuperMarketInfo;
import com.scrx.ymg.entity.YmgHead;

@Repository("headDao")
@Transactional
public class HeadDaoImpl extends BaseDaoImpl implements IHeadDao {
	
	@Autowired
	private IAgentInfoDao agentdao;
    
	@Autowired
	private IDirectDao directdao;
	
	@Autowired
	private ISeckillDao seckilldao;
	
	@Autowired
	private ISuperMarketDao superdao;

	public HeadWithdarw getHeadWithdarw() {
		try {
			Query query = getCurrentSession().createQuery("from HeadWithdarw where id=1");
			return (HeadWithdarw) query.uniqueResult();
		} catch (Exception e) {
			return null;

		}
	}


	public String addParentGoodsType(GoodsType goodsType) {
		try {
			getCurrentSession().save(goodsType);
			return "true";
		} catch (Exception e) {
			return "false";
		}
		
		
	}


	public String addOffspringGoodsType(GoodsType goodsType) {

		return null;
	}
	
	public GoodsType findOne(int id) {
		Query query = getCurrentSession().createQuery("from GoodsType where id=?");
		query.setParameter(0, id);
		return (GoodsType) query.uniqueResult();
	}



	public String updateGoodsType(GoodsType goods) {
		
		try {
			getCurrentSession().update(goods);
			return "true";
		} catch (Exception e) {
			return "false";
		}
		
	}



	public String updateAgentInfo(AgentInfo info) {
		try {
	        this.getCurrentSession().update(info);
	        Query query = getCurrentSession().createQuery("update SuperMarketInfo set paymentMode=? where agentId=?");
	        query.setParameter(0, info.getPaymentMode());
	        query.setParameter(1, info.getId());
	        query.executeUpdate();
	        return "true";
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return "false";
		}
	}


	public DividedInto selectDividedInto() {
		try {
			Query query = getCurrentSession().createQuery("from DividedInto where id=1"); 
			return (DividedInto)query.uniqueResult();
		} catch (Exception e) {
			return null;
		}
		

	}

	public String updateDividedInto(DividedInto into) {
             try {
				this.getCurrentSession().update(into);
				return "true";
			} catch (Exception e) {
				return "false";
			}
	}



	public AgentDrawings getDrawingsById(String transferNo) {
		try {
			Query query = getCurrentSession().createQuery("from AgentDrawings where id=:id");
			query.setParameter("id", Integer.parseInt(transferNo));
			AgentDrawings agentDrawings=(AgentDrawings)query.uniqueResult();
			int typeId=agentDrawings.getTypeId();
			String addId="";
			String userName="";
			String phone="";
			if(typeId==1){//超市
				SuperMarketInfo info =superdao.findByID(agentDrawings.getMerchantId());
				addId=info.getIdentityCard();
				userName=info.getPrincipal();
				phone=info.getPhone();
            }else if(typeId==2){//代理商
            	AgentInfo info=agentdao.getAgentInfoByID(agentDrawings.getMerchantId());
            	addId=info.getAddId();
				userName=info.getPrincipal();
				phone=info.getPhone();
            }else if(typeId==3){//异业
            	OtherInfo info=seckilldao.getOtherInfoById(agentDrawings.getMerchantId());
            	addId=info.getOtherCard();
				userName=info.getOtherPrincipal();
				phone=info.getOtherPhone();
            }else if(typeId==4){//直营
            	 DirectInfo info=directdao.getDirectInfoById(agentDrawings.getMerchantId());
	            	addId=info.getUserCard();
					userName=info.getUserName();
					phone=info.getPhone();
            }
			agentDrawings.setAddId(addId);
			agentDrawings.setPhone(phone);
			agentDrawings.setUserName(userName);
			return agentDrawings;
		} catch (Exception e) {
			return null;
		}
	}



	public BankAccount getBankAccountBySuperId(Integer merchantId) {
		try {
			Query query = getCurrentSession().createQuery("from BankAccount where superId=:id");
			query.setParameter("id", merchantId);
			return (BankAccount)query.uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	public BankAccount getBankAccountByAgentId(Integer merchantId) {
		try {
			Query query = getCurrentSession().createQuery("from BankAccount where agentId=:id");
			query.setParameter("id", merchantId);
			return (BankAccount)query.uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	public BankAccount getBankAccountByOtherId(Integer merchantId) {
		try {
			Query query = getCurrentSession().createQuery("from BankAccount where otherId=:id");
			query.setParameter("id", merchantId);
			return (BankAccount)query.uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}
	
	public BankAccount getBankAccountByDirectId(Integer merchantId) {
		try {
			Query query = getCurrentSession().createQuery("from BankAccount where directId=:id");
			query.setParameter("id", merchantId);
			return (BankAccount)query.uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}




	public HeadMoney getHeadMoney() {
		try {
			Query query = getCurrentSession().createQuery("from HeadMoney where id=1");
			return (HeadMoney)query.uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}



	public String updateAgentDrawings(AgentDrawings drawing) {
		try {
			getCurrentSession().update(drawing);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}


	@Override
	public String updateReturnMoney(AgentDrawings drawing, Object obj, HeadMoney headMoney) {
		try {
			getCurrentSession().update(drawing);
			getCurrentSession().update(obj);
			getCurrentSession().update(headMoney);
			return "true";
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return "false";
		}
	}



	public String updateYmgHead(YmgHead head) {
		try {
			getCurrentSession().update(head);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}



	public YmgHead getHeadInfo() {
		Query query = getCurrentSession().createQuery("from YmgHead where id=1");
		return (YmgHead)query.uniqueResult();
	}



	
}
